<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
             label-width="110px">
      <!--<el-form-item label="发货通知编号" prop="tongzhiNo">
          <el-input v-model="queryParams.tongzhiNo" placeholder="请输入发货通知编号" clearable
              @keyup.enter.native="handleQuery" />
      </el-form-item>-->
      <el-form-item label="发货单号" prop="fahuoNo">
        <el-input v-model="queryParams.fahuoNo" placeholder="请输入发货单号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单编号" prop="proNo">
        <el-input v-model="queryParams.proNo" placeholder="请输入订单编号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品编号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入产品编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入产品名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="产品型号" prop="models">
        <el-input v-model="queryParams.models" placeholder="请输入产品型号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="客户" prop="models">
        <el-select v-model="queryParams.params['kehuId']" placeholder="请选择客户" filterable 
                   clearable style="width: 100%">
          <el-option v-for="dict in dict.type.goods_customer" :data="dict.raw" ref="myselectCustomerForPrj"
                     :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="到货日期">
        <el-date-picker v-model="dateRange"
                        style="display: flex;"
                        value-format="yyyy-MM-dd"
                        type="daterange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" >导出
        </el-button>
      </el-form-item>
    </el-form>

    <div v-if="selectedRows.length > 0">
      <div>已勾选数量合计: {{ selectedRows.reduce((acc, row) => acc + row.shuliang, 0) }}</div>
      <div>已勾选金额合计: {{ selectedRows.reduce((acc, row) => acc + row.money, 0) }}</div>
    </div>
    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight" @sort-change="handleSortChange" :summary-method="totalOutYear"
              show-summary>
      <el-table-column type="selection" width="60" align="center"/>
      <el-table-column sortable label="客户" align="center" prop="params.customName" show-overflow-tooltip/>
      <el-table-column sortable label="订单编号" align="center" prop="proNo" show-overflow-tooltip/>
      <el-table-column sortable align="center" label="发货单号" prop="fahuoNo">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击查看详情" effect="dark" placement="left-start">
            <span @click="xiangqing(scope.row)" style="color: #00afff;cursor:pointer;">
              {{ scope.row.fahuoNo }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column sortable label="产品编号" align="center" prop="nos" show-overflow-tooltip/>
      <el-table-column sortable label="产品名称" align="center" prop="name" show-overflow-tooltip/>
      <el-table-column sortable label="产品型号" align="center" prop="models" show-overflow-tooltip/>
      <el-table-column label="单位" align="center" prop="danwei"/>
      <el-table-column label="产品单价" align="center" prop="danjian"/>
      <el-table-column sortable label="发货状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yifahuo_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column sortable label="发货日期" align="center" prop="fahuoDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fahuoDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="交货日期" align="center" prop="jiaohuoDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jiaohuoDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="发货数量" align="center" prop="shuliang"/>
      <el-table-column sortable label="发货金额" align="center" prop="money"/>
      <el-table-column sortable label="制单人" align="center" prop="createBy"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button @click="qualityTestingFahuo(scope.row)" icon="el-icon-files"
                     size="mini" type="text">产品质检
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <lvYifahuomingxi-form ref="form" @ok="getList"></lvYifahuomingxi-form>
    <factory-test-quality-fahuo-detail ref="qualityTestingFahuoDetail" @ok="getList"></factory-test-quality-fahuo-detail>
    <lv-yifahuomingxi-list-module ref="yifahuoDetail"></lv-yifahuomingxi-list-module>
  </div>
</template>

<script>
import {deleteAction, getAction, accAdd, downloadFile} from '@/api/manage'
import LvYifahuomingxiForm from '@/views/develop/lvYifahuomingxi/module/LvYifahuomingxiForm'
import FactoryTestQualityFahuoDetail from "@/views/develop/factory/factoryTestQuality/FactoryTestQualityFahuoDetail.vue";
  import LvYifahuomingxiListModule from "@/views/develop/lvYifahuomingxi/module/LvYifahuomingxiListModule.vue";

export default {
  dicts: ['yifahuo_status','goods_customer'],
  name: "LvYifahuomingxiList",
  components: {
    FactoryTestQualityFahuoDetail,
    LvYifahuomingxiForm,
    LvYifahuomingxiListModule
  },
  props: {
    projectCode: {
      type: [String],
      required: true,
      default: ''
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发货单明细表格数据
      dataList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tongzhiId: null,
        tongzhiNo: null,
        jiaohuoDate: null,
        caigouNo: null,
        gonghuoNo: null,
        proId: null,
        proNo: null,
        prodName: null,
        saleId: null,
        name: null,
        models: null,
        mingxi: null,
        nos: null,
        danwei: null,
        danjian: null,
        shuliang: null,
        money: null,
        status: null,
        fahuoId: null,
        fahuoNo: null,
        params:{},
        orderByColumn: "nos",
        isAsc: "descending"
      },
      url: {
        list: '/lvYifahuomingxi/lvYifahuomingxi/listNoCommit',
        remove: '/lvYifahuomingxi/lvYifahuomingxi/',
        export: '/lvYifahuomingxi/lvYifahuomingxi/export',
      },
      tableMaxHeight: window.innerHeight - 320,
      selectedRows: [], // 用于存储勾选的行数据
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  watch: {
    'projectCode': {
      handler(newVal, oldVal) {
        console.log("projectCode", newVal);
        if (newVal) {
          this.queryParams.proNo = newVal;
          this.getList();
        }
      },
      deep: true,
      immediate: true
    },
  },
  methods: {
     xiangqing(row) {
      var data = row
       data.id = row.fahuoId
      this.$refs.yifahuoDetail.showMingxi(data);
    },
    /** 查询发货单明细列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange,"FahuoDate")
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.selectedRows = selection;
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    totalOutYear(param) {
      const {columns, data} = param;
      const sums = [];

      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "money" || column.property === "shuliang"|| column.property === "danjian") {

          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    qualityTestingFahuo(row){
      this.$refs.qualityTestingFahuoDetail.showByFahuoDetail(row,true);
    },

    handleExport(){
      let that = this;
      that.loading = false;
      that.$confirm('是否确认导出已发货明细表?', "确认", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        downloadFile('/lvYifahuomingxi/lvYifahuomingxi/export', that.queryParams);
      });
    }


  }
};
</script>
